package com.pengtu.gsj.controller.gcjs;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.pengtu.gsj.controller.base.BaseController;
import com.pengtu.gsj.dao.base.Page;
import com.pengtu.gsj.dao.base.QueryFilter;
import com.pengtu.gsj.entity.app.Attachment;
import com.pengtu.gsj.entity.app.Sysmenu;
import com.pengtu.gsj.interceptor.Token;
import com.pengtu.gsj.service.system.SysmenuManagerService;
import com.pengtu.gsj.service.system.SystemManageService;
import com.pengtu.gsj.utils.UpDownFileUtils;
import com.pengtu.gsj.utils.web.SpringMvcUtils;

@Controller
@RequestMapping("/recuperation/")
public class RecuperationController extends BaseController {
	
	@Autowired
	private SysmenuManagerService sysmenuManagerService;
	
	@Autowired
	private SystemManageService systemManageService;
	
	/**
	 * 
	 * loadEntity:
	 * 适用: 每个方法执行之前都会执行该方法
	 * @param id  菜单主键id
	 * @param model 绑定参数信息
	 * @return 返回持久对象SystemMenu或者新创建对象
	 * @exception 
	 * @since  1.0.0
	 */
	@ModelAttribute
	public void loadEntity(String id, Model model) {
		Sysmenu sysmenu ;
		Attachment attachment;
		String left = SpringMvcUtils.getParameter("left");
		if (StringUtils.isNotEmpty(left)) {
			sysmenu = sysmenuManagerService.get(left);
		}else {
			sysmenu = new Sysmenu();
		}
		if (StringUtils.isNotEmpty(id)) {
			attachment = systemManageService.getAttachment(id);
		}else {
			attachment = new Attachment();
		}
		model.addAttribute("attachment", attachment);
		model.addAttribute("list", sysmenu.getChildList());
		model.addAttribute("top", SpringMvcUtils.getParameter("top"));
		model.addAttribute("left", SpringMvcUtils.getParameter("left"));
		model.addAttribute("tag", SpringMvcUtils.getParameter("tag"));
		model.addAttribute("parentId", SpringMvcUtils.getParameter("parentId"));
	}
	
	/**
	 * 
	 * showRecuperation:
	 * 适用:显示附件列表页面
	 * @param model 
	 * @return 附件列表页面
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping("listInfo")
	@Token(save = true)
	public String showRecuperation(Model model) {
		String msg = SpringMvcUtils.getParameter("msg");
		System.out.println("msg:"+msg);
		if (StringUtils.isNotEmpty(msg)) {
			model.addAttribute("msg", msg);
		}
		return "gcjs/recuperation_list";
	}
	
	/**
	 * 
	 * getRecuperationList:
	 * 适用:动态获取日常巡查的附件信息
	 * @param page  分页信息
	 * @param request
	 * @return 返回某一页的数据
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value="list",produces = "application/json; charset=utf-8")
	@ResponseBody
	public Page<Attachment>  getRecuperationList(Page<Attachment> page,HttpServletRequest request) {
		QueryFilter queryFilter = QueryFilter.buildFromHttpRequest(request);
		if (!page.isOrderBySetted()) {
			page.setOrderBy("upfileFileName");
			page.setOrder(Page.DESC);
		}
		page = systemManageService.getAttachmentList(page, queryFilter);
		return page;
	}
	
	/**
	 * 
	 * saverecuperationFile:
	 * 适用:保存日常监管模块的文件上传
	 * @param attachment  接受页面传递过来的附件信息
	 * @param request
	 * @param attributes   绑定提示信息
	 * @return  重定向到附件列表页面
	 * @throws Exception 
	 * @exception 
	 * @since  1.0.0
	 */
	@RequestMapping(value = "save",method=RequestMethod.POST)
	@ResponseBody
	@Token(remove = true)
	public String saveRecuperationFile(@RequestParam(value = "file", required = false) MultipartFile multipartFile,@ModelAttribute Attachment attachment,HttpServletRequest request,RedirectAttributes attributes) throws Exception {
		MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
		String tag = SpringMvcUtils.getParameter("tag");
		System.out.println("tag:"+tag);
		String type = multipartRequest.getParameter("type").trim();
		Sysmenu sysmenu = sysmenuManagerService.findSysmenuByTag(tag);
		String parentPath = "";
		if (sysmenu != null) {
			parentPath = sysmenuManagerService.getParentNames(sysmenu);
		}
		attachment = UpDownFileUtils.getAttachmentByFile(multipartFile,attachment, parentPath+type);
		systemManageService.saveAttachment(attachment);
		attributes.addFlashAttribute("msg", "文件上传成功!");
		
		/*绑定参数到重定向地址*/
		attributes.addAttribute("top",	SpringMvcUtils.getParameter("top"));
		attributes.addAttribute("left", SpringMvcUtils.getParameter("left"));
		attributes.addAttribute("LId", SpringMvcUtils.getParameter("LId"));
		attributes.addAttribute("tag", tag);
		attributes.addAttribute("parentId", SpringMvcUtils.getParameter("parentId"));
		return "redirect:/recuperation/listInfo.do";
	}
}
